<template>
    <div>
        <van-row style="margin-top: 1rem;"> 
            <van-col span="2" >
                <van-icon name="arrow-left" @click="exit" />
            </van-col>
            
        </van-row>
        <van-row style="margin-bottom: 3rem;">
            <van-col span="6">
                &nbsp;
            </van-col>
            <van-col span="12">
                修改密码
            </van-col>
            <van-col span="6">
                &nbsp;
            </van-col>
        </van-row>
        <van-row>
            <van-col span="24">
                <van-form @submit="onSubmit">
                    <van-field
                        v-model="username"
                        name="username"
                        label="账号"
                        placeholder="账号"
                        :rules="[{ required: true, message: '请填写账号' }]"
                        v-show="false"
                    />
                    <van-field
                        v-model="password"
                        type="password"
                        name="password"
                        label="新密码"
                        placeholder="新密码"
                        :rules="[{ required: true, message: '请填写新密码' }]"
                    />
                    <van-field
                        v-model="repassword"
                        type="password"
                        name="repassword"
                        label="确认密码"
                        placeholder="确认密码"
                        :rules="[{ required: true, message: '请填写密码' }]"
                    />
                    <div style="margin: 16px;">
                        <van-button round block type="info" native-type="submit">提交</van-button>
                    </div>
                    </van-form>
            </van-col>
        </van-row>
    </div>
</template>

<script>
    import { Toast } from 'vant';
    import {repassword} from '@/api/index'
    export default {
        data() {
            return {
                username: localStorage.getItem('username'),
                password: '',
                repassword:'',
            };
  },
  methods: {
    onSubmit(values) {
        if(values.password===values.repassword){
            repassword(values).then(res=>{
                if(res.data)
                {
                    Toast("修改成功")
                    this.$router.push("/")
                }
                    
                else 
                    Toast("修改失败")
            })
        }   
        else{
            Toast.fail('密码不一致');
        }
        
    },
    exit(){
        this.$router.push('/my')
    },
    // setUserName(){
    //     this.username=localStorage.getItem('username')
    // }
  },
  mounted(){
    // this.setUserName()
  }
};
</script>